package com.pzhu.dormitory.controller;

import java.util.Arrays;
import java.util.Map;


import com.pzhu.dormitory.entity.CostEntity;
import com.pzhu.dormitory.entity.CostVo;
import com.pzhu.dormitory.vo.CostWVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.pzhu.dormitory.entity.CostwEntity;
import com.pzhu.dormitory.service.CostwService;
import com.pzhu.dormitory.utils.PageUtils;
import com.pzhu.dormitory.utils.Rb;
import com.pzhu.dormitory.utils.R;


/**
 * 网费
 *
 * @author 
 * @email 
 * @date 2021-05-09 07:39:28
 */
@Slf4j
@RestController
@Api(value = "CostwController", tags = "网费")
@RequestMapping("dormitory/costw")
public class CostwController {
    @Autowired
    private CostwService costwService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取网费list")
    public R<CostwEntity> list(@RequestParam Map<String, Object> params) {
        PageUtils<CostwEntity> page = costwService.queryPage(params);
        return Rb.a(CostwEntity.class).success().page(page).build();
    }

    /**
     * 列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "page")
    public R<CostWVo> page(@RequestParam Map<String, Object> params) {
        PageUtils<CostWVo> page = costwService.queryPageCostVo(params);
        return Rb.a(CostWVo.class).success().page(page).build();
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取一个网费信息")
    public R<CostwEntity> info(@Validated @PathVariable("id") Long id) {
            CostwEntity costw = costwService.getById(id);
        return Rb.a(CostwEntity.class).success().withData(costw).build();
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存网费信息")
    @PostMapping("/save")
    public R<CostwEntity> save(@Validated @RequestBody CostwEntity costw) {
            costwService.save(costw);
        return Rb.a(CostwEntity.class).success().build();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新网费信息")
    @PutMapping("/update")
    public R<CostwEntity> update(@RequestBody CostwEntity costw) {
        CostwEntity costEntity = costwService.getById(costw.getId());
        costw.setCost(costw.getCost().add(costEntity.getCost()));
        costwService.updateById(costw);
        return Rb.a(CostwEntity.class).success().build();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除网费信息")
    @DeleteMapping("/delete")
    public R<CostwEntity> delete(@RequestBody Long[] ids) {
            costwService.removeByIds(Arrays.asList(ids));
        return Rb.a(CostwEntity.class).success().build();
    }

}
